package sys.knot.model;

import javax.servlet.http.HttpServletRequest;

public class HttpServletRequestIndata implements Indata {
    private static final String HEADER_PREFIX = "header.";
    private static final String SPLITTER = "\\.";

    private HttpServletRequest request;
    private String serviceEndPoint;
    private String serviceMethod;

    //TODO handle URL-path-parameters? REST-like?
    //TODO move impl. to knot-server, keep interface here...

    public HttpServletRequestIndata(HttpServletRequest request) {
        this.request = request;
        this.serviceEndPoint = request.getPathInfo();
        this.serviceMethod = request.getMethod();
    }

    public String getParameter(String key) {
        String result;
        if (key.startsWith(HttpServletRequestIndata.HEADER_PREFIX)) {
            String headerKey = key.split(HttpServletRequestIndata.SPLITTER, 2)[1];
            result = request.getHeader(headerKey);
        } else {
            result = request.getParameter(key);
        }
        return result;
    }

    public String getServiceEndPoint() {
        return serviceEndPoint;
    }

    public String getServiceMethod() {
        return serviceMethod;
    }
}
